<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\appclass\model\CardList;
use app\appclass\model\CardUser;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassCoach;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\appclass\model\Lable;
use app\appclass\model\StoreInfo;
use app\payclass\model\Record;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


class AdminList extends AdminRest
{

    protected $model;

    protected $cate_model;

    protected $lable_model;

    protected $room_model;

    protected $date_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model       = new ClassList();

        $this->cate_model  = new ClassCate();

        $this->lable_model = new ClassLable();

        $this->room_model  = new ClassRoom();

        $this->date_model  = new ClassDate();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-20 11:34
     * @功能说明:后台数据概览
     */
    public function index(){

        $input = $this->_param;

        $record_model = new ClassRecord();

        $date_model   = new ClassDate();

        $card_user_model = new CardUser();

        $dis['uniacid'] = $this->_uniacid;

        if(!empty($input['store_id'])){

            $dis['store_id'] = $input['store_id'];
        }


        $where  = $where1 = [];

        if(!empty($input['start_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $where[] = ['create_time','between',"$start_time,$end_time"];

            $where1[] = ['start_time','between',"$start_time,$end_time"];
        }

        //总的销售金额
        $total_count = $card_user_model->getSaleCount($dis,$where);
        //卡销售金额
        $list[0]['sale_price'] = $card_user_model->getSaleCountOrPrice($dis,$where);
        //卡销售数量
        $list[0]['sale_count'] = $card_user_model->getSaleCountOrPrice($dis,$where,'',1);
        //
        $list[0]['balance']    = !empty($total_count)?round($list[0]['sale_count']/$total_count*100,2):0;

        $list[0]['title']      = '售卡';
        //停卡金额
        $list[1]['sale_price'] = $card_user_model->saleOrCancelPrice($dis,$where);
        //停卡数量
        $list[1]['sale_count'] = $card_user_model->getSaleOrCancel($dis,$where,1,1);
        //
        $list[1]['balance']    = !empty($total_count)?round($list[1]['sale_count']/$total_count*100,2):0;

        $list[1]['title']      = '退卡';

        $list = array_values($list);

        $data['index'] = $list;
        //今日预约人数
        $data['today_app']  = $record_model->where($dis)->whereTime('create_time','today')->where('status','in',[1,3])->sum('num');
        //会员人数
        $data['card_user_count']  = $card_user_model->where($dis)->where(['is_cancel'=>0])->group('user_id')->count();
        //新增访客
        $data['new_user_count']   = $card_user_model->todayCustomer($input['store_id']);
        //课程数量
        $data['class_count']      = $record_model->classCount($dis,$where1);
        //排课数量
        $data['class_date_count'] = $date_model->classDateCount($dis,$where);
        //教练数量
        $data['coach_count']      = $date_model->coachCount($dis,$where1);

        $total_card = $card_user_model->getSaleCountOrPrice($dis,$where,'',1);

        $arr = [
            //期限卡
            'date_card',
            //次数卡
            'times_card',
            //储值卡
            'cash_card'
        ];

        foreach ($arr as $k=>$value){

            $data[$value]['count']  = $card_user_model->getSaleCountOrPrice($dis,$where,'',1,$k);

            $data[$value]['balance']= !empty($total_card)?round($data[$value]['count']/$total_card*100,2):0;

            $data[$value]['price']  = $card_user_model->getSaleCountOrPrice($dis,$where,'',0,$k);

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 17:50
     * @功能说明:课程列表
     */

    public function classList(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        if(!empty($input['store_id'])){

            $dis['store_id'] = $input['store_id'];
        }

        if(!empty($input['cate_id'])){

            $dis['cate_id'] = $input['cate_id'];
        }

        $data = $this->model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:45
     * @功能说明:添加课程
     */
    public function classAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->adminDataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:48
     * @功能说明:编辑课程
     */
    public function classUpdate(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->adminDataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 14:35
     * @功能说明:课程上下架
     */
    public function classStatusUpdate(){

        $input = $this->_input;


        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:52
     * @功能说明:课程详情
     */
    public function classInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->dataInfo($dis);

        $res['cate_type'] = $this->cate_model->where(['id'=>$res['cate_id']])->value('type');

        if(!empty($res['class_card'])){

            $card_model = new CardList();

            foreach ($res['class_card'] as &$v){

                $v['card_name'] = $card_model->where(['id'=>$v['card_id']])->value('title');

                $v['card_type'] = $card_model->where(['id'=>$v['card_id']])->value('type');

            }
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:22
     * @功能说明:教练列表
     */
    public function coachSelect(){

        $input = $this->_param;

        $card_model = new CardList();

        $data = $card_model->coach($input['store_id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:24
     * @功能说明:教室列表
     */
    public function classroomSelect(){

        $input = $this->_param;

        $room_model = new ClassRoom();

        $dis = [

            'uniacid' => $this->_uniacid,

            'store_id'=> $input['store_id'],

            'status'  => 1
        ];

        $data = $room_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:36
     * @功能说明:标签列表
     */
    public function lableSelect(){

        $input = $this->_param;

        $lable_model = new Lable();

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1,

            'pid'     => 0
        ];

        $data = $lable_model->where($dis)->group('id desc')->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['list'] = $lable_model->where(['pid'=>$v['id']])->where('status','=',1)->order('id desc')->select()->toArray();

            }

        }

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 10:40
     * @功能说明:类型下拉框
     */
    public function cateSelect(){

        $input = $this->_param;

        $cate_model = new ClassCate();

        $dis = [

            'uniacid' => $this->_uniacid,

            'store_id'=> $input['store_id'],

            'status'  => 1
        ];

        $data = $cate_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 17:15
     * @功能说明:课程排课列表
     */
    public function classDateList(){

        $this->date_model->cancelfailClass($this->_uniacid);

        $input = $this->_param;

        $i = 0;

        while ($i<7){

            $str[] = date('Y-m-d',$i*86400+$input['start_time']).' '.changeWeek(date('w',$i*86400+$input['start_time']));

            $i++;
        }

        $start_time = strtotime(date('Y-m-d',$input['start_time']));

        $end_time   = $start_time+86400*7;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        $dis[] = ['a.start_time','between',"$start_time,$end_time"];

        if(!empty($input['store_id'])){

            $dis[] = ['a.store_id','=',$input['store_id']];
        }

        $info = $this->date_model->getStartTimeGroup($dis);

       // dump($info);exit;

        if(!empty($info)){

            foreach ($info as $k=> $value){

                $where = [];

                $where[] = ['a.uniacid','=',$this->_uniacid];

                $where[] = ['a.start_time','between',"$start_time,$end_time"];

                $where[] = ['a.hour','=',$value];

                $where[] = ['a.status','>',-1];

                if(!empty($input['store_id'])){

                    $where[] = ['a.store_id','=',$input['store_id']];
                }

                $date = $this->date_model->dataList($where);

                $dates = [];

                if(!empty($date)){

                    foreach ($date as $vs){

                        $day = date('Y-m-d',$vs['start_time']).' '.changeWeek(date('w',$vs['start_time']));

                        $dates[$day][] = $vs;
                    }

                    $dates['start_time'] = $value;

                    $list[] = $dates;

                }

            }
        }

        $list = !empty($list)?$list:[];

        $lists['list'] = $list;

        $lists['time'] = $str;

        return $this->success($lists);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 11:47
     * @功能说明:添加课程排课
     */
    public function classDateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->date_model->classDataAdd($input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 11:59
     * @功能说明:编辑排课
     */

    public function classDateUpdate(){

        $input = $this->_input;

        $res = $this->date_model->classDataSave($input,$input['start_time'],$input['end_time']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 13:39
     * @功能说明:排课详情
     */
    public function classDataInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->date_model->dataInfo($dis);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 17:03
     * @功能说明:课程列表
     */
    public function classSelect(){

        $input = $this->_param;

        $cate_model = new ClassCate();

        $dis = [

            'uniacid' => $this->_uniacid,

            'store_id'=> $input['store_id'],

            'status'  => 1,

        ];

        if(!empty($input['type'])){

            $dis['type'] = 0;
        }

        $list = $cate_model->where($dis)->order('id desc')->select()->toArray();

        if(!empty($list)){

            foreach ($list as $k=> $value){

                $dis = [

                    'status' => 1,

                    'uniacid'=> $this->_uniacid,

                    'cate_id'=> $value['id'],

                ];

                $class = $this->model->where($dis)->order('top desc,id desc')->select()->toArray();

                if(!empty($class)){

                    $data[$k]['title'] = $value['title'];

                    $data[$k]['id']    = $value['id'];

                    $data[$k]['children'] = $class;
                }

            }

        }

        $data = !empty($data)?array_values($data):[];

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-18 15:01
     * @功能说明:场地信息
     */
    public function storeInfo(){

        $input = $this->_param;

        $store_model = new StoreInfo();

        $dis = [

            'store_id' => $input['store_id'],

            'uniacid'  => $this->_uniacid
         ];

        $data = $store_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-18 15:06
     * @功能说明:修改门店
     */
    public function storeUpdate(){

        $input = $this->_input;

        $store_model = new StoreInfo();

        $dis = [

            'store_id' => $input['store_id'],

        ];

        $input['business_license'] = !empty($input['business_license'])?implode(',',$input['business_license']):'';

        $data = $store_model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-18 17:45
     * @功能说明:门店下面的员工列表
     */
    public function storeStaffList(){

        $input = $this->_param;

        $card_model = new CardList();

        $name = !empty($input['name'])?$input['name']:'';

        $dis = [];

        if(!empty($name)){

            $dis[] = ['name','like','%'.$name.'%'];
        }

        $data = $card_model->coachList($input['store_id'],$dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-10 17:27
     * @功能说明:修改店主的权限（店主店员）
     */
    public function updateCoachAuth(){

        $input = $this->_input;

        $user_model = new IndexUserInfo();

        $dis = [

          'fans_id' => $input['fans_id']
        ];

        $res = $user_model->where($dis)->update(['is_class'=>$input['is_class']]);

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-10 17:27
     * @功能说明:修改店主的权限（店主店员）
     */
    public function updateCoachStatus(){

        $input = $this->_input;

        $user_model = new IndexUserInfo();

        $dis = [

            'fans_id' => $input['fans_id']
        ];

        $res = $user_model->where($dis)->update(['class_status'=>$input['class_status']]);

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-15 14:40
     * @功能说明:删除排课
     */
    public function delClassDate(){

        $input = $this->_input;

        $res = $this->date_model->delClassDate($input['id']);

        return $this->success($res);

    }






}
